package com.gopay.webtest.handler.citicSign;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 中信银行签名工具类
 * @CiticSignUtils.java
 * @author dong-wang5@hnari.com
 * @2016年6月16日 下午4:28:54  www.gopay.com.cn Inc.All rights reserved.
 */
public class CiticSignUtils
{
	private static Logger logger = Logger.getLogger(CiticSignUtils.class);
	/**
	 * 
	 * @param message
	 * @return
	 * 针对支付宝基金报文签名
	 * 
	 * 单元测试状态:通过
	 */
	public static String cartFundSignature(String message)
	{
		String signed = message;
		try 
		{
//			String keyFileName = CiticSignUtils.getValueByName("cart_fundsign");
			String keyFileName = "cart_fundsign.cer";
			InputStream privateCer = CiticSignUtils.class.getClassLoader().getResourceAsStream(keyFileName);
			String priKeyString = KeyIO.readKeyString(privateCer);
			signed = Certificate.sign(AlgorithmEnum.RSA, priKeyString, message);
			if(privateCer != null)
			{
				privateCer.close();
			}
			
		} 
		catch (IOException e) 
		{
			logger.error("", e);
		}
		return signed;
	}
	/**
	 * 
	 * @param message
	 * @param signed
	 * @return
	 * 针对支付宝基金报文验签
	 * 
	 * 单元测试状态:通过
	 */
	public static boolean cartFundVerify(String message,String signed)
	{
		String pubKeyString="";
		try 
		{
			String keyFileName = CiticSignUtils.getValueByName("cart_fundverify");
			InputStream publicCer = CiticSignUtils.class.getClassLoader().getResourceAsStream(keyFileName);
		    pubKeyString = KeyIO.readKeyString(publicCer);
			publicCer.close();
			if(publicCer != null)
			{
				publicCer.close();
			}
		} 
		catch (IOException e) 
		{
			logger.error("", e);
		}
		return Certificate.verify(AlgorithmEnum.RSA, pubKeyString, message, signed);
	}
	/**
	 * 此方法是通过指定的名称获取属性文件中对应的值，并返回
	 * @param name 对应文件cartoon.properties中的键值
	 */
	public static String getValueByName(String name)
	{
		InputStream is = CiticSignUtils.class.getClassLoader().getResourceAsStream("cartoon.properties");
		Properties prop = new Properties();
		try 
		{
			prop.load(is);
		} catch (IOException e)
		{
			logger.error("", e);
		}
		String value = prop.getProperty(name);
		if(is != null)
		{
			try 
			{
				is.close();
			} 
			catch (IOException e) 
			{
				logger.error("", e);
			}
		}
		return value;
	}

	public static void main(String[] args) throws Exception
	{
/*		InputStream is = Utils.class.getClassLoader().getResourceAsStream("student.xml");
		byte[] bytes = IOUtils.toByteArray(is);
		InputStream iss = IOUtils.toInputStream(new String(bytes));
		String value =  getElementValueFromXmlIns(iss,"name");
		logger.info("value:"+value);*/
//		String str = Utils.XML2PairStr(Utils.class.getResourceAsStream("test.xml"), new String[]{"name","age","mm"}, true);
//		String str = Utils.PairStr2XML("_input_charset=gbk&bank_card=&partner=2088001159940003&service=purchase_single&trade_time=20100322 17:23:48", null, null);
//		logger.info(str);
		//String[] pairs = "  =aa".split("=");
		//System.out.println(pairs[0].length());
		 String message = "alipay_bank_id=95200155300000588&file_name=htf_allocation_20110606.txt" +
 		"&file_path=supervise-allocationf/20110606f_allocation_20110606.txt&file_type=ALIPAY-ALLOCATION" +
 		"&file_upload_time=20110608110109&memo=&return_url=" +
 		"&serial_no=20110608548965&service=file_upload_notify";
		 
		String signed = CiticSignUtils.cartFundSignature(message);
		System.out.println("signed-->【" + signed);
		message = message+"&sign="+signed+"&sign_type=RSA";
		String signed1 ="ijz/5a/P3m0a4xnYOw9gcdHn6RFlT6InSqnyc91yNJcXcaMCmv4xxFDzxcnb4CURFuczTqgsmdhCgO7Vg5KKb+3XM9d1wF5U/xUGs8Yu1i+4hU7ZROHNtZh5tY9wYRzTviYcwSTMu62SpvZ2t3xeVTXfi9weXMC7XgpSNMAvcpY=";
		
		System.out.println(CiticSignUtils.cartFundVerify(message, signed));
		//logger.info(Utils.cartFundVerify(message, signed));
	}
}
